// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   LoginAction.java

package com.iphaeton.lms.action.user;

import com.iphaeton.common.helper.UserSession;
import com.iphaeton.common.helper.UserTrackArtifact;
import com.iphaeton.common.util.DateUtil;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.action.organize.UserBizSupportAction;
import com.iphaeton.lms.business.organize.IUserService;
import com.iphaeton.lms.pojo.organize.*;
import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.apps.ajaxchat.dto.UserDTO;

public class LoginAction extends UserBizSupportAction
{

    public LoginAction()
    {
    }

    public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String fwdValue = null;
        DynaActionForm loginForm = (DynaActionForm)form;
        String userId = (String)loginForm.get("userId");
        String password = (String)loginForm.get("password");
        User currUser = null;
        try
        {
            if(userId != null && userId.length() > 0)
                currUser = user.doLogin(userId, password);
            if(currUser != null && currUser.isvalidUser())
            {
                Group manageGroup = null;
                List list = user.findGroup(currUser, "m");
                if(list.size() == 1)
                    manageGroup = (Group)list.get(0);
                UserSession userSession = new UserSession();
                userSession.setId(currUser.getId());
                userSession.setOrgId(currUser.getOrganizeId());
                userSession.setOrgUrl(currUser.getOrganize().getUrl());
                userSession.setUserId(currUser.getUserId());
                userSession.setUserName(currUser.getName());
                UserDTO userdto = new UserDTO(currUser.getName() + "[" + currUser.getUserId() + "]");
                userdto.setLastAJAXRequest(new Date());
                userSession.setUserChatDTO(userdto);
                userSession.setRole(user.findSysRole(currUser));
                userSession.setUerTrackInfo(pushUserTrackInfo(request));
                if(manageGroup != null)
                    userSession.setManageOrgId(manageGroup.getOrganizeId());
                logger.debug("CurrentUserManageOrganizeId:" + userSession.getManageOrgId());
                request.getSession().setAttribute("userSession", userSession);
                fwdValue = "success";
            }
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        return mapping.findForward(fwdValue);
    }

    public ActionForward logout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        request.getSession().invalidate();
        return mapping.findForward("login");
    }

    private UserTrackArtifact pushUserTrackInfo(HttpServletRequest request)
    {
        UserTrackArtifact uTrackInfo = new UserTrackArtifact();
        uTrackInfo.setIpAddr(request.getRemoteAddr());
        uTrackInfo.setHostName(request.getRemoteHost());
        uTrackInfo.setSessionId(request.getSession().getId());
        uTrackInfo.setLogonTime(DateUtil.toDate(request.getSession().getCreationTime()));
        String Agent = request.getHeader("User-Agent");
        StringTokenizer st = new StringTokenizer(Agent, ";");
        st.nextToken();
        return uTrackInfo;
    }

    private static Logger logger;

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.user.LoginAction.class.getName());
    }
}
